package com.woniuxy.shiroinitest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class ShiroPermIniTest {
    public static void main(String[] args) {
        //配置ini shiro环境
        BasicIniEnvironment basicIniEnvironment=new BasicIniEnvironment("classpath:shiroini/shiro_prem.ini");
        //通过环境创建一个SecurityManager
        SecurityManager securityManager=basicIniEnvironment.getSecurityManager();
        //给工具类中注入SecurityManager,表示这个工具类是基于这个环境生成的
        SecurityUtils.setSecurityManager(securityManager);
        //通过环境生成主体对象
        Subject subject=SecurityUtils.getSubject();
        //主题进行登录，然后SecurityManager内部自己去环境匹配账号密码
        subject.login(new UsernamePasswordToken("lipeilin","123456"));

        //判断该用户是否拥有某个角色
        System.out.println("判断该用户是否拥有role1角色"+subject.hasRole("role1"));
        //判断该用户是否拥有某个权限
        System.out.println("判断该主体是否拥有user.update权限"+subject.isPermitted("user.update"));
        if(subject.isAuthenticated()){
            //权限的粒度不同！
            if(subject.hasRole("role1")){
                System.out.println(subject.getPrincipal()+"继续执行");
            }
        }
    }
}
